package cn.school.iot.service.impl;

import cn.school.common.rabbitMQ.RabbitSenderService;
import cn.school.common.utils.SysUserUtils;
import cn.school.iot.dao.StuFaceGateRuleDao;
import cn.school.iot.eneity.StudentFaceGateRuleDo;
import cn.school.iot.service.StuFaceGateRuleService;
import cn.school.iot.vo.FaceGateRuleTimePeriodReceiveVo;
import cn.school.iot.vo.FaceGateRuleTimePeriodVo;
import cn.school.iot.vo.StuGateRuleParam;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description: 学生闸机规则Service
 * @ClassName: StuFaceGateRuleServiceImpl
 * @Author: tzw
 * @Date: 2024/4/9 16:20
 */
@Slf4j
@Service
public class StuFaceGateRuleServiceImpl extends ServiceImpl<StuFaceGateRuleDao, StudentFaceGateRuleDo> implements StuFaceGateRuleService {
    @Resource
    private RabbitSenderService rabbitSenderService;
    /**
     * 添加或修改学生绑定规则
     * @param stuGateRuleParam
     * @return 添加结果
     */
    @Override
    @Transactional
    public boolean addOrUpdateDeviceRule(StuGateRuleParam stuGateRuleParam) {
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        log.info("学生绑定规则：开始处理数据");
        boolean result = false;
        List<FaceGateRuleTimePeriodReceiveVo> receiveVos = new ArrayList<>();
        List<StudentFaceGateRuleDo> faceGateRuleDoList = new ArrayList<>();
        for (FaceGateRuleTimePeriodVo selectRule : stuGateRuleParam.getSelectRules()) {
            FaceGateRuleTimePeriodReceiveVo receiveVo = new FaceGateRuleTimePeriodReceiveVo();
            receiveVo.setDeviceType(selectRule.getDeviceType());
            receiveVo.setFgrpCode(selectRule.getFgrpCode());
            receiveVo.setCreateTime(Calendar.getInstance().getTime());
            receiveVo.setCreateBy(SysUserUtils.getSysUser().getUsername());
            receiveVos.add(receiveVo);
            for (String id : stuGateRuleParam.getIds()) {
                StudentFaceGateRuleDo studentFaceGateRuleDo = new StudentFaceGateRuleDo();
                studentFaceGateRuleDo.setStudentId(id);
                studentFaceGateRuleDo.setDeviceType(selectRule.getDeviceType());
                studentFaceGateRuleDo.setFgrpCode(selectRule.getFgrpCode());
                studentFaceGateRuleDo.setCreateTime(Calendar.getInstance().getTime());
                studentFaceGateRuleDo.setCreateBy(SysUserUtils.getSysUser().getUsername());
                faceGateRuleDoList.add(studentFaceGateRuleDo);
            }

        }
        //删除学生绑定的旧规则
        LambdaQueryWrapper<StudentFaceGateRuleDo> wrapper = new LambdaQueryWrapper<>();
        List<String> stuIds = new ArrayList<>(Arrays.asList(stuGateRuleParam.getIds()));
        wrapper.in(StudentFaceGateRuleDo::getStudentId,stuIds);
        super.remove(wrapper);
        //添加新的绑定
        if (faceGateRuleDoList.size() > 0){
            result = this.saveOrUpdateBatch(faceGateRuleDoList);
            log.info("学生规则绑定：数据入库成功");
        }else{
            log.info("学生规则绑定：没有数据需要入库");
        }
        Map map = new HashMap();
        map.put("ids", stuGateRuleParam.getIds());
        map.put("vo",receiveVos.get(0));
        //type：4 绑定学生
        map.put("type","4");

        //发送消息到Iot
        try {
//            rabbitSenderService.sendMqMessage(JSON.toJSONString(map),null);
            rabbitSenderService.sendMqMessage(JSON.toJSONString(map), null,
                    "zhxypt-" + tenantId + "-exchanges",
                    "facegate-" + tenantId + "-routing");
            log.info("绑定规则：消息发送到路由Key {} 成功","facegate-" + tenantId + "-routing");
        } catch (Exception e) {
            log.error("绑定规则：消息发送到Iot失败->"+e.getMessage());
        }
        return result;
    }

}
